home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / database / msgobj10.zip / MSGOBJPI.CPP < prev    next >
C/C++ Source or Header  |  1993-03-25  |  12KB  |  291 lines

  1. // MsgObjPi.CPP procedures to handle PIP compressed strings (type 10 messages)
  2.  
  3. #include <fcntl.h>
  4. #include <io.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "config.h"
  8. #include "msgobj.h"
  9.  
  10. char *pip[128]={ // decompression table
  11. "PREFIX","SEEN-BY: ","MSGID: ","PATH: ",": ",
  12. "zion","ment","---","che","chi","ghe","ghi","str","","il","al","ed",
  13. "pr","st","..","  ",", ",". ","; ","++",
  14. "a'","e'","i'","o'","u'","a ","e ",
  15. "i ","o ","u ","nt","hi",
  16. "bb","ba","be","bi","bo","bu",
  17. "cc","ca","ce","ci","co","cu",
  18. "dd","da","de","di","do","du",
  19. "ff","fa","fe","fi","fo","fu",
  20. "gg","ga","ge","gi","go","gu",
  21. "ll","la","le","li","lo","lu",
  22. "mm","ma","me","mi","mo","mu",
  23. "nn","na","ne","ni","no","nu",
  24. "pp","pa","pe","pi","po","pu",
  25. "rr","ra","re","ri","ro","ru",
  26. "ss","sa","se","si","so","su",
  27. "tt","ta","te","ti","to","tu",
  28. "vv","va","ve","vi","vo","vu",
  29. "zz","za","ze","zi","zo","zu",
  30. "==",":-","' ","ha","ho","qu","#"};
  31.  
  32.  
  33.  
  34. // compression table for Pip compressor
  35.  
  36. // >0: best; len=2
  37. // 0: no compression
  38. // -1: special handling
  39.  
  40. char piptable[26][26]=
  41. // a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z 
  42. {{ 0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  43. {  38, 37,  0,  0, 39,  0,  0,  0, 40,  0,  0,  0,  0,  0, 41,  0,  0,  0,  0,  0, 42,  0,  0,  0,  0,  0},
  44. {  44,  0, 43,  0, 45,  0,  0, -1, 46,  0,  0,  0,  0,  0, 47,  0,  0,  0,  0,  0, 48,  0,  0,  0,  0,  0},
  45. {  50,  0,  0, 49, 51,  0,  0,  0, 52,  0,  0,  0,  0,  0, 53,  0,  0,  0,  0,  0, 54,  0,  0,  0,  0,  0},
  46. {  0 ,  0,  0, 16,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  47. {  56,  0,  0,  0, 57, 55,  0,  0, 58,  0,  0,  0,  0,  0, 59,  0,  0,  0,  0,  0, 60,  0,  0,  0,  0,  0},
  48. {  62,  0,  0,  0, 63,  0, 61, -1, 64,  0,  0,  0,  0,  0, 65,  0,  0,  0,  0,  0, 66,  0,  0,  0,  0,  0},
  49. { 124,  0,  0,  0,  0,  0,  0,  0, 36,  0,  0,  0,  0,  0,125,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  50. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 14,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  51. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  52. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  53. {  68,  0,  0,  0, 69,  0,  0,  0, 70,  0,  0, 67,  0,  0, 71,  0,  0,  0,  0,  0, 72,  0,  0,  0,  0,  0},
  54. {  74,  0,  0,  0, -1,  0,  0,  0, 76,  0,  0,  0, 73,  0, 77,  0,  0,  0,  0,  0, 78,  0,  0,  0,  0,  0},
  55. {  80,  0,  0,  0, 81,  0,  0,  0, 82,  0,  0,  0,  0, 79, 83,  0,  0,  0,  0, 35, 84,  0,  0,  0,  0,  0},
  56. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  57. {  86,  0,  0,  0, 87,  0,  0,  0, 88,  0,  0,  0,  0,  0, 89, 85,  0,  0,  0,  0, 90,  0,  0,  0,  0,  0},
  58. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,126,  0,  0,  0,  0,  0},
  59. {  92,  0,  0,  0, 93,  0,  0,  0, 94,  0,  0,  0,  0,  0, 95,  0,  0, 91,  0,  0, 96,  0,  0,  0,  0,  0},
  60. {  98,  0,  0,  0, 99,  0,  0,  0,100,  0,  0,  0,  0,  0,101,  0,  0,  0, 97, -1,102,  0,  0,  0,  0,  0},
  61. { 104,  0,  0,  0,105,  0,  0,  0,106,  0,  0,  0,  0,  0,107,  0,  0,  0,  0,103,108,  0,  0,  0,  0,  0},
  62. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  63. { 110,  0,  0,  0,111,  0,  0,  0,112,  0,  0,  0,  0,  0,113,  0,  0,  0,  0,  0,114,109,  0,  0,  0,  0},
  64. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  65. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  66. {  0 ,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  67. { 116,  0,  0,  0,117,  0,  0,  0, -1,  0,  0,  0,  0,  0,119,  0,  0,  0,  0,  0,120,  0,  0,  0,  0,115}};
  68.  
  69.  
  70. void message_frame::pipstring(unsigned char huge*s,int f)
  71. {
  72.   register int best;
  73.   char l,k;
  74.   unsigned char b[2],z;
  75.   *b=128;
  76.   while (*s)
  77.     {
  78.       if (*s==10)
  79.         s++;
  80.       else
  81.         {
  82.           best=127; l=1;
  83.           if(*s>96 && *s<123)
  84.             {
  85.               if(*(s+1)>96 && *(s+1)<123)
  86.                 {
  87.                   if((k=piptable[*s-'a'][*(s+1)-'a'])!=0)
  88.                     {
  89.                       if (k==-1) // special multi-letter compression
  90.                         {
  91.                           switch(*s)
  92.                             {
  93.                               case 'c': switch(*(s+1)) {
  94.                                           case 'h': if (*(s+2)=='i') {best=9; l=3;} else if (*(s+2)=='e') {best=8; l=3;}; break;
  95.                                         } break;
  96.                               case 'g': switch(*(s+1)) {
  97.                                           case 'h': if (*(s+2)=='i') {best=11; l=3;} else if (*(s+2)=='e') {best=10; l=3;}; break;
  98.                                         } break;
  99.                               case 'm': switch(*(s+1)) {
  100.                                           case 'e': if((*(s+2)=='n') && (*(s+3)=='t')) {best=6; l=4;} else {best=75; l=2;} break;
  101.                                         } break;
  102.                               case 's': switch(*(s+1)) {
  103.                                           case 't': best=18; l=2; if (*(s+2)=='r') {best=12; l=3;} break;
  104.                                         } break;
  105.                               case 'z': switch(*(s+1)) {
  106.                                           case 'i': best=118; l=2; if ((*(s+2)=='o') && (*(s+3)=='n')) {best=5; l=4;} break;
  107.                                         } break;
  108.                             }
  109.                         }
  110.                       else
  111.                         { // normal multiletter compression
  112.                           best=k; l=2;
  113.                         }
  114.                     }
  115.                 }
  116.               else
  117.                 { // special letter+symbol compression
  118.                   switch(*s)
  119.                     {
  120.                       case 'a': switch(*(s+1)) {
  121.                                   case ' ': best=30; l=2; break;
  122.                                   case '\'': best=25; l=2; break;} break;
  123.                       case 'e': switch(*(s+1)) {
  124.                                   case ' ': best=31; l=2; break;
  125.                                   case 'd': best=16; l=2; break;
  126.                                   case '\'': best=26; l=2; break;} break;
  127.                       case 'i': switch(*(s+1)) {
  128.                                   case ' ': best=32; l=2; break;
  129.                                   case 'l': best=14; l=2; break;
  130.                                   case '\'': best=27; l=2; break;} break;
  131.                       case 'o': switch(*(s+1)) {
  132.                                   case ' ': best=33; l=2; break;
  133.                                   case '\'': best=28; l=2; break;} break;
  134.                       case 'u': switch(*(s+1)) {
  135.                                   case ' ': best=34; l=2; break;
  136.                                   case '\'': best=29; l=2; break;} break;
  137.                     }
  138.                 }
  139.             }
  140.           else
  141.             switch(*s)
  142.               {
  143.                 case ' ': if (*(s+1)==' ') {best=20; l=2;} break;
  144.                 case '-':if ((*(s+1)=='-') && (*(s+2)=='-')) {best=7; l=3;} break;
  145.                 case '\'': if (*(s+1)==' ') {best=123; l=2;} break;
  146.                 case '+': if (*(s+1)=='+') {best=24; l=2;} break;
  147.                 case ';': if (*(s+1)==' ') {best=23; l=2;} break;
  148.                 case ',': if (*(s+1)==' ') {best=21; l=2;} break;
  149.                 case '=': if (*(s+1)=='=') {best=121; l=2;} break;
  150.                 case ':': switch(*(s+1)) {
  151.                             case '-': best=122; l=2; break;
  152.                             case ' ': best=4; l=2; break;} break;
  153.                 case '.': switch(*(s+1)) {
  154.                             case '.': best=19; l=2; break;
  155.                             case ' ': best=22; l=2; break;} break;
  156.                 case 'M':if ((*(s+1)=='S') &&
  157.                              (*(s+2)=='G') &&
  158.                              (*(s+3)=='I') &&
  159.                              (*(s+4)=='D') &&
  160.                              (*(s+5)==':') &&
  161.                              (*(s+6)==' ')) {best=2; l=7;} break;
  162.                 case 'P':if ((*(s+1)=='A') &&
  163.                              (*(s+2)=='T') &&
  164.                              (*(s+3)=='H') &&
  165.                              (*(s+4)==':') &&
  166.                              (*(s+5)==' ')) {best=3; l=6;} break;
  167.                 case 'S':if ((*(s+1)=='E') &&
  168.                              (*(s+2)=='E') &&
  169.                              (*(s+3)=='N') &&
  170.                              (*(s+4)=='-') &&
  171.                              (*(s+5)=='B') &&
  172.                              (*(s+6)=='Y') &&
  173.                              (*(s+7)==':') &&
  174.                              (*(s+8)==' ')) {best=1; l=9;} break;
  175.               }
  176.           if (best==127)
  177.             {if ((*s<128) || (*s==141)) write(f,s,1); else {b[1]=*s-127; write(f,b,2);} s++;}
  178.           else
  179.             {z=128+best; write(f,&z,1); s+=l;}
  180.         }
  181.     }
  182.   write(f,"",1);
  183. }
  184.  
  185.  
  186. void message_frame::unpipstring(char cr,unsigned char huge*s,int f)
  187. {
  188.   unsigned char t;
  189.   char *p;
  190.   while (*s)
  191.     {
  192.       if (*s!=10)
  193.         {
  194.           if ((*s==141) || (*s==13)) *s=cr;
  195.           if (*s<128)
  196.             write(f,s,1);
  197.           else
  198.             {
  199.               if (*s==128)
  200.                 {s++; t=(*s)+127; write(f,&t,1);}
  201.               else
  202.                 write(f,p=pip[*s-128],strlen(p));
  203.             }
  204.         }
  205.       s++;
  206.     }
  207. }
  208.  
  209.  
  210. void message_frame::unpipfile(unsigned char huge*s,int f)
  211. {
  212.   char *t;
  213.   while(::read(f,s,1),*s!=0)
  214.     {
  215.       if (*s>128)
  216.         {
  217.           t=pip[*s-128];
  218.           while (*t) {*s=*t; s++; t++;}
  219.           *s=0; s--;
  220.         }
  221.       else
  222.         if (*s==128)
  223.           {
  224.             ::read(f,s,1);
  225.             *s+=127;
  226.           }
  227.       s++;
  228.     }
  229. }
  230.  
  231.  
  232. int message_frame::read0(unsigned char huge*s, int f,unsigned int mxlen,char truncate)
  233. {
  234.   while (mxlen && (::read(f,s,1),*s) && !eof(f)) {s++; mxlen--;}
  235.   if(*s==0) return 0;
  236.   *s=0;
  237.   if(truncate)
  238.     while ((::read(f,s,1),*s) && !eof(f));
  239.   return -1;
  240. }
  241.  
  242.  
  243. int message_frame::readfile(unsigned char huge*s,char *fn)
  244. {
  245.   int f;
  246.   #ifdef __WINDOWS__
  247.     if ((f=OpenFile(fn,O_RDONLY|O_BINARY|O_DENYWRITE))==-1) return -1;
  248.   #else
  249.     if ((f=open(fn,O_RDONLY|O_BINARY|O_DENYWRITE))==-1) return -1;
  250.   #endif
  251.   while (!eof(f)) {::read(f,s,1); if (*s!=10) s++;}
  252.   s--; /* to skip last *s, that is a EOF */
  253.   if (*(s-1)==26)
  254.     {*(s-1)=13; *s=0;}
  255.   else
  256.     {*s=13; *(s+1)=0;}
  257.   return(close(f));
  258. }
  259.  
  260.  
  261. void message_frame::write0(unsigned char huge*s,int f)
  262. {
  263.   write(f,s,strlen((char huge*)s)+1);
  264. }
  265.  
  266.  
  267. void message_frame::writemex(unsigned char huge*s,int f,char ctrl_a)
  268. {
  269.   unsigned char b;
  270.   while (*s) 
  271.     {
  272.       b=((*s&127)==13)?'\n':((*s==1)?ctrl_a:*s);
  273.       write(f,&b,1);
  274.       s++;
  275.     }
  276. }
  277.  
  278.  
  279. char huge*message_frame::find_origin(char huge*msg)
  280. {
  281.   char huge*s1=msg,huge*s2=NULL,huge*s3;
  282.   while((s3=strstr(s1," * Origin:"))!=NULL)
  283.     {
  284.       s1=s3+1;
  285.       if((*(s3-1)==13) || (*(s3-1)==10) || ((unsigned char)(*(s3-1))==141))
  286.         s2=s3;
  287.     }
  288.   return s2;
  289. }
  290.  
  291.